﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace MauThuTabControl
{
    /// <summary>
    /// Interaction logic for NH.xaml
    /// </summary>
    public partial class NH : Page
    {
        ConnectionDB cn = new ConnectionDB();
        int _CoThaoTac = 0;
        public NH()
        {
            InitializeComponent();
            LoadGW();
            EnableButton(true,false);
            EnablaeTextbox(false);
        }

      
        
        //private void Expander_Collapsed(object sender, RoutedEventArgs e)
        //{
        //    _dtgdonvitinh.IsEnabled = true;
        //    _dtgdonvitinh.UnselectAll();

        //    //this.Height = 350;
        //}

        //private void Expander_Expanded(object sender, RoutedEventArgs e)
        //{
        //    _dtgdonvitinh.IsEnabled = false;
        //    //this.Height = 450;
        //}

        public void EnableButton(bool b1,bool b2) 
        {
            _btthem.IsEnabled = b1;
            _btsua.IsEnabled = b1;
            _btxoa.IsEnabled = b1;
            _dtgdonvitinh.IsEnabled = b1;
            _btuu.IsEnabled = b2;
            _bthuy.IsEnabled = b2;
        }

        public void EnablaeTextbox(bool b) 
        {
            _txtdvt.IsEnabled = b;
            _txtmdvt.IsEnabled = b;
        }

        public void LoadGW() 
        {
            DataTable dt = cn.LoadData("select * from DMDonViTinh");
            _dtgdonvitinh.ItemsSource = dt.DefaultView;
            
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
            //_btsua.Visibility = System.Windows.Visibility.Hidden;
            //_bthuysua.Visibility = System.Windows.Visibility.Hidden;
           
        }



        private void _btxoa_Click(object sender, RoutedEventArgs e)
        {
           
            int row = _dtgdonvitinh.SelectedIndex;
            if (row == -1)
            {
                MessageBox.Show("Bạn phải chọn một dòng cần xóa!", "Chú Ý!");
            }
            else
            {
                string ma = ((DataRowView)_dtgdonvitinh.SelectedItem).Row[0].ToString();
                if (MessageBox.Show("Bạn có chắc muốn xóa không?", "Chú ý!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    cn._ThucThiCauTruyVan("DELETE FROM DMDonViTinh WHERE maDVT=N'" + ma + "'");
                    LoadGW();
                }

            }
        }

       

        private void _btsua_Click(object sender, RoutedEventArgs e)
        {
            _CoThaoTac = 2;
          
            int row = _dtgdonvitinh.SelectedIndex;
            if (row == -1)
            {
                MessageBox.Show("Bạn phải chọn một dòng cần sửa!", "Chú Ý!");
            }
            else
            {

                EnableButton(false, true);
                EnablaeTextbox(true);
                _txtmdvt.IsReadOnly = true;
                _txtdvt.Focus();

            }
        }

       
        private void _btthem_Click(object sender, RoutedEventArgs e)
        {
            _CoThaoTac = 1;
            _dtgdonvitinh.UnselectAll();
            EnableButton(false, true);
            EnablaeTextbox(true);
            _txtmdvt.IsReadOnly = false;
            _txtdvt.Text = "";
            _txtmdvt.Text = "";
            _txtmdvt.Focus();
            //_txtmdvt.IsReadOnly = false;
            
            //cn._ThucThiCauTruyVan("INSERT INTO DMDonViTinh(maDVT,tenDVT)VALUES('" + _txtmdvt.Text + "',N'" + _txtdvt.Text + "')");
                   
        }

        private void _bthuy_Click(object sender, RoutedEventArgs e)
        {


            EnableButton(true, false);
            EnablaeTextbox(false);
            if (_dtgdonvitinh.Items.Count > 0) 
            {
                _dtgdonvitinh.SelectedIndex = 0;
            }
           

        }

        private void _dtgdonvitinh_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_dtgdonvitinh.SelectedIndex != -1)
            {
                _txtmdvt.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["maDVT"].ToString();
                _txtdvt.Text = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["tenDVT"].ToString();
            }
        }

        private void _btuu_Click(object sender, RoutedEventArgs e)
        {
            if (_kiemtra() == true)
            {
                if (_CoThaoTac == 1)
                {
                    string _Insert = "INSERT INTO DMDonViTinh(maDVT,tenDVT)VALUES('" + _txtmdvt.Text + "',N'" + _txtdvt.Text + "')";
                    cn._ThucThiCauTruyVan(_Insert);
                }
                if (_CoThaoTac == 2)
                {
                    if (MessageBox.Show("Bạn có chắc muốn sửa không?", "Chú ý!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        string _Update = "UPDATE DMDonViTinh SET tenDVT=N'" + _txtdvt.Text + "' WHERE maDVT='" + _txtmdvt.Text + "'";
                        cn._ThucThiCauTruyVan(_Update);
                    }
                }

                LoadGW();
                //Khóa các nút
                EnableButton(true, false);
                //Khóa các textbox
                EnablaeTextbox(false);
            }
            else { MessageBox.Show("Không được bỏ trống","Thông báo"); }
        }


        public bool _kiemtra() 
        {
            if (_txtmdvt.Text == "" || _txtdvt.Text == "") 
            {
                return false;
            }
            return true;
        }

       

     

        //private void _dtgdonvitinh_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        //{
        //    if (e.EditAction == DataGridEditAction.Commit)
        //    {
        //        string _txtmdvt = (((DataRowView)_dtgdonvitinh.SelectedItem).Row["maDVT"] as TextBox).Text;
        //        string _txtdvt = (((DataRowView)_dtgdonvitinh.SelectedItem).Row["tenDVT"] as TextBox).Text;
        //        cn._ThucThiCauTruyVan("UPDATE DMDonViTinh SET tenDVT=N'" + _txtdvt + "' WHERE maDVT='" + _txtmdvt + "'");
        //        DataTable dt = cn.LoadData("select * from DMDonViTinh");
        //        _dtgdonvitinh.ItemsSource = dt.DefaultView;

        //    }
        //}

     

       
    }
}
